//
//  RechargeViewController.m
//  Consume100
//
//  Created by eliot on 16/3/29.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "RechargeViewController.h"
#import "PayDetailTableViewCell.h"
#import "PaySucceedViewController.h"

@interface RechargeViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnViewTopDistance;

@property (weak, nonatomic) IBOutlet UILabel *numberLb;


@property (weak, nonatomic) IBOutlet UIView *btnView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnViewH;


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *panBtn;

@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) NSMutableArray *lbArr;
@end


@implementation RechargeViewController

+(RechargeViewController*)CreatRechargeViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    RechargeViewController *RechargeViewController=[story instantiateViewControllerWithIdentifier:@"RechargeViewController"];
    return RechargeViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBtnView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PayDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"PayDetailTableViewCell"];
    self.panBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    self.panBtn.layer.cornerRadius = 6;
    self.panBtn.layer.masksToBounds = YES;
    self.title = @"充值金币";
    /** 设置导航啦样式*/
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
    
}

- (void)setupBtnView {
    self.btnArr = [NSMutableArray array];
    self.lbArr = [NSMutableArray array];
    self.btnViewTopDistance.constant = 0;
    self.btnViewH.constant = (Width - 50)/115.0*83.0 + 50;
    self.btnView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    for (int i = 0; i <= 8; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [self.btnView addSubview:btn];
        btn.layer.cornerRadius = 6;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(clickAmountOfChargeBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor =  [UIColor whiteColor];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo((Width - 50)/115.0*83.0/3.0);
            make.width.mas_equalTo((Width - 50) / 3);
            if (i % 3 == 0) {
                make.left.mas_equalTo(15);
                if (i / 3 == 0) {
                    make.top.mas_equalTo(15);
                }else {
                    make.top.mas_equalTo((10 +(Width - 50)/115.0*83.0/3.0) * (i/3) + 15);
                }
            }
            if (i % 3 == 1) {
                make.left.mas_equalTo((Width - 50) / 3 + 25);
                if (i / 3 == 0) {
                    make.top.mas_equalTo(15);
                }else {
                    make.top.mas_equalTo((10 +(Width - 50)/115.0*83.0/3.0) * (i/3) + 15);
                }
            }
            if (i % 3 == 2) {
                make.right.mas_equalTo(-15);
                if (i / 3 == 0) {
                    make.top.mas_equalTo(15);
                }else {
                    make.top.mas_equalTo((10 +(Width - 50)/115.0*83.0/3.0) * (i/3) + 15);
                }
            }
        }];
        UILabel *lb1 = [[UILabel alloc] init];
        UILabel *lb2 = [[UILabel alloc] init];
        [btn addSubview:lb1];
        [btn addSubview:lb2];
        [self.btnArr addObject:btn];
        [lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo((Width - 50)/115.0*83.0/3.0 / 2 - 20);
            make.height.mas_equalTo(20);
        }];
        [lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(lb1.mas_bottom);
            make.height.mas_equalTo(20);
        }];
        lb1.font = [UIFont systemFontOfSize:14];
        lb2.font = [UIFont systemFontOfSize:12];
        lb1.textAlignment = NSTextAlignmentCenter;
        lb2.textAlignment = NSTextAlignmentCenter;
        switch (i) {
            case 0:
            {
                lb1.text = @"10元";
                lb2.text = @"金币:100个";
            }
                break;
            case 1:
            {
                lb1.text = @"20元";
                lb2.text = @"金币:200个";
            }
                break;
            case 2:
            {
                lb1.text = @"30元";
                lb2.text = @"金币:300个";
            }
                break;
            case 3:
            {
                lb1.text = @"50元";
                lb2.text = @"金币:500个";
            }
                break;
            case 4:
            {
                lb1.text = @"100元";
                lb2.text = @"金币:1000个";
                
            }
                break;
            case 5:
            {
                lb1.text = @"200元";
                lb2.text = @"金币:2000个";
            }
                break;
            case 6:
            {
                lb1.text = @"300元";
                lb2.text = @"金币:3000个";
            }
                break;
            case 7:
            {
                lb1.text = @"500元";
                lb2.text = @"金币:5000个";
            }
                break;
            default:
            {
                lb1.text = @"1000元";
                lb2.text = @"金币:10000个";
            }
                break;
        }
        [self.lbArr addObject:lb1];
        [self.lbArr addObject:lb2];
    }
    
}


- (void)clickAmountOfChargeBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.backgroundColor =  [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
        UILabel *lb1 = self.lbArr[sender.tag * 2];
        UILabel *lb2 = self.lbArr[sender.tag * 2 + 1];
        lb1.textColor = [UIColor whiteColor];
        lb2.textColor = [UIColor whiteColor];
        for (int i = 0; i <= 8; i ++) {
            if (i != sender.tag) {
                UIButton *btn = self.btnArr[i];
                btn.backgroundColor = [UIColor whiteColor];
            }
        }
        for (UILabel *lb in self.lbArr) {
            if (lb != lb1 && lb != lb2) {
                lb.textColor = [UIColor blackColor];
            }
        }
        
    }else {
        sender.backgroundColor =  [UIColor whiteColor];
        UILabel *lb1 = self.lbArr[sender.tag * 2];
        UILabel *lb2 = self.lbArr[sender.tag * 2 + 1];
        lb1.textColor = [UIColor blackColor];
        lb2.textColor = [UIColor blackColor];
    }
    


}

- (IBAction)clickpayBtn:(id)sender {
    
    PaySucceedViewController *vc = [PaySucceedViewController CreatPaySucceedViewController];
    vc.isFromFirstPage = NO;
    [self creatNav:vc];
    
}

//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
        PayDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PayDetailTableViewCell" forIndexPath:indexPath];
        cell.checkIV.hidden = YES;
        cell.bgView.layer.borderWidth = 1;
        cell.bgView.layer.borderColor = [[UIColor grayColor] CGColor];
        cell.bgView.layer.cornerRadius = 12.5;
        cell.bgView.layer.masksToBounds = YES;
        if (indexPath.row == 1) {
            cell.iconIV.image = [UIImage imageNamed:@"iocn_alipay"];
            cell.titleLb.text = @"支付宝支付";
        }else if (indexPath.row == 2){
            cell.iconIV.image = [UIImage imageNamed:@"icon_wechat"];
            cell.titleLb.text = @"微信支付";
        }
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PayDetailTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.checkIV.hidden = NO;
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    PayDetailTableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    cell.checkIV.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
